﻿using CommunityToolkit.Mvvm.ComponentModel;
using ContactSMS.Client.Models;
using ContactSMS.Client.Service;
using System.Collections.ObjectModel;

namespace ContactSMS.Client.ViewModels
{
    public partial class ContactDetailViewModel : ObservableObject
    {
        [ObservableProperty]
        private ObservableCollection<CallLogModel> callLog;
        [ObservableProperty]
        private ObservableCollection<CallLogModelNew> callLogs;
        [ObservableProperty]
        private ObservableCollection<PhoneModel> phoneList;
        [ObservableProperty]
        private ContactInfoModel contact;
        private readonly ContactService contactService;
        private readonly PhoneRecordService phoneRecordService;

        public ContactDetailViewModel(ContactService contactService, PhoneRecordService phoneRecordService)
        {
            callLog = new ObservableCollection<CallLogModel>();
            phoneList = new ObservableCollection<PhoneModel>();
            //LoadPhone();
            //LoadCallLog();
            this.contactService = contactService;
            this.phoneRecordService = phoneRecordService;
        }

        public async Task LoadContact(long ContactId)
        {
            try
            {
                var result = await contactService.GetByIdAsync(ContactId);
                if (result != null && result.Success)
                {
                    Contact = result.Data;
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Error", result?.Message ?? "Failed to load contacts", "OK");
                }
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }

        public async Task LoadCallLogs(long ContactId)
        {
            try
            {
                var result = await phoneRecordService.GetPhoneRecordsAsync(ContactId);
                if (result != null && result.Success)
                {
                    //CallLogs = result.Data.List;
                    CallLogs = new ObservableCollection<CallLogModelNew>(result.Data.List);
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Error", result?.Message ?? "Failed to load contacts", "OK");
                }
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }

        void LoadPhone()
        {
            PhoneList = new ObservableCollection<PhoneModel>
            {
                new PhoneModel
                {
                    Id = 1,
                    Phone = "18861737878",
                    Tag = "其他",
                    Address = "江苏苏州",
                    Type = "移动",
                    TagAddressType = "其他 - 江苏苏州 移动"
                },
                new PhoneModel
                {
                    Id= 2,
                    Phone = "18861737178",
                    Tag = "住宅",
                    Address = "江苏苏州",
                    Type = "移动",
                    TagAddressType = "其他 - 江苏苏州 移动"
                },
                new PhoneModel
                {
                    Id= 3,
                    Phone = "18161737878",
                    Tag="手机",
                    Address = "江苏苏州",
                    Type = "移动",
                    TagAddressType = "其他 - 江苏苏州 移动"
                },
                new PhoneModel
                {
                    Id= 4,
                    Phone = "12861737878",
                    Tag="其他",
                    Address = "江苏苏州",
                    Type = "移动",
                    TagAddressType = "其他 - 江苏苏州 移动"
                },
                new PhoneModel
                {
                    Id= 5,
                    Phone = "13861737878",
                    Tag="其他",
                    Address = "江苏苏州",
                    Type = "移动",
                    TagAddressType = "其他 - 江苏苏州 移动"
                },
            };
        }

        void LoadCallLog()
        {
            CallLog = new ObservableCollection<CallLogModel>
            {
                // 可能是 "Missed", "Incoming", 或 "Outgoing"
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Missed"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Incoming"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                },
                new CallLogModel
                {
                    Phone = "18861737878",
                    Date = "2024-07-07 14:54",
                    Duration = "2",
                    Type = "Outgoing"
                }
            };
        }
    }
}
